import axios from 'axios';

const API_BASE_URL = 'http://localhost:5000/api/bondbacktest';

// 创建axios实例
const api = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json',
  },
});

// 执行回测
export const runBacktest = async (backtestParams) => {
  try {
    const response = await api.post('/backtest', backtestParams);
    return response.data;
  } catch (error) {
    throw error.response ? error.response.data : { success: false, error: '网络请求失败' };
  }
};

// 获取报告列表
export const getReportList = async () => {
  try {
    const response = await api.get('/reports');
    return response.data;
  } catch (error) {
    throw error.response ? error.response.data : { success: false, error: '获取报告列表失败' };
  }
};

// 获取单个报告URL
export const getReportUrl = (reportId) => {
  return `${API_BASE_URL}/reports/${reportId}`;
};

// 删除指定报告
export const deleteReport = async (reportId) => {
  try {
    const response = await api.delete(`/reports/${reportId}`);
    return response.data;
  } catch (error) {
    throw error.response ? error.response.data : { success: false, error: '删除报告失败' };
  }
};

// 债券标的模糊查询
export const searchBondSymbols = async (query) => {
  try {
    const response = await api.get(`/bond/symbol/search?query=${encodeURIComponent(query)}`);
    return response.data;
  } catch (error) {
    throw error.response ? error.response.data : { success: false, error: '债券标的查询失败' };
  }
};

export default {
  runBacktest,
  getReportList,
  getReportUrl,
  deleteReport,
  searchBondSymbols
};
